package com.zha.boss.mvc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zha.boss.constants.SysConstants;
import com.zha.boss.dao.entity.PayWay;
import com.zha.boss.dao.result.DicItem;
import com.zha.boss.enums.resp.RespCode;
import com.zha.boss.mvc.convert.PayWayConvert;
import com.zha.boss.mvc.models.forms.PayWayConfigForm;
import com.zha.boss.mvc.models.resp.AjaxResult;
import com.zha.boss.mvc.models.resp.PageInfo;
import com.zha.boss.mvc.service.PayWayService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zha
 * @CreateTime: 2024年09月15日 13:43
 */
@RestController
public class PayWayController {
    @Resource
    private PayWayService payWayService;
    @Resource
    private PayWayConvert payWayConvert;
    @GetMapping("/pay/way/pageList")
    public AjaxResult payPageList(Integer pageNo,String productCode) {
        pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
        IPage<PayWay> payWayIPage = payWayService.pageList(productCode,pageNo, SysConstants.PAGE_SIZE);
        // 获取查询结果的数据
        PageInfo pageInfo = new PageInfo(payWayIPage.getCurrent(), payWayIPage.getPages());
        List<PayWay> payWayIPageRecords = payWayIPage.getRecords();

        Map<String, Object> data = new HashMap<>();
        data.put("page", pageInfo);
        data.put("list", payWayConvert.toListPayWayVo(payWayIPageRecords));
        return AjaxResult.ok(data);
    }

    @GetMapping("/pay/way/dic")
    public AjaxResult payWayDicList() {
        List<DicItem> dicItemList = payWayService.queryPayWayDicList();
        return AjaxResult.ok(dicItemList);
    }
    @GetMapping("/pay/way/type/dic")
    public AjaxResult payWayTypeDicList(String wayCode) {
         if (StrUtil.isNotBlank(wayCode) && wayCode.trim().length() > 5){
             List<DicItem> dicItemList = payWayService.queryPayTypeDicList(wayCode);
             return AjaxResult.ok(dicItemList);
         }
         return AjaxResult.warn(RespCode.REQUEST_PARAM_ERR);
    }

    @PostMapping("/pay/way/config")
    public AjaxResult addProductPayWayConfig(@RequestBody PayWayConfigForm payWayConfigForm){
        payWayService.addPayWayConfig(payWayConfigForm);
        return AjaxResult.ok();
    }
    /**
     * 删除支付方式
     */
    @PostMapping("/pay/way/remove")
    public AjaxResult removePayWay(String id) {
        if (StrUtil.isNotBlank(id) && id.trim().length() > 10){
            payWayService.removePayWay(id);
            return AjaxResult.ok();
        }
        return AjaxResult.warn(RespCode.REQUEST_PARAM_ERR);
    }
}
